<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>

    <meta charset="utf-8">

    <title>摄像头信息管理</title>
    <!--引入相关的js文件-->
    <link href="../backPage/ui/bootstrap-3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="../backPage/ui/bootstrap-table-1.11.0/bootstrap-table.min.css" rel="stylesheet"/>
    <link href="../backPage/ui/waves-0.7.5/waves.css" rel="stylesheet"/>
    <link href="../backPage/ui/jquery-confirm/jquery-confirm.min.css" rel="stylesheet"/>
    <link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>

    <script src="../backPage/ui/js/jquery-3.3.1.min.js"></script>
    <script src="../backPage/ui/waves-0.7.5/waves.js"></script>
    <script src="../backPage/ui/jquery-confirm/jquery-confirm.min.js"></script>
    <script src="../backPage/ui/bootstrap-3.3.0/js/bootstrap.min.js"></script>
    <script src="../backPage/ui/bootstrap-table-1.11.0/bootstrap-table.min.js"></script>
</head>

<body>
<div id="main">
    <div id="toolbar">
        <button class="btn btn-default" href="javascript:;" onclick="createAction()">新增摄像头信息</button>
        <button class="btn btn-default" href="javascript:;" onclick="updateAction()">编辑摄像头信息</button>
        <button class="btn btn-default" href="javascript:;" onclick="deleteAction()">删除摄像头信息</button>
        <button class="btn btn-default" href="javascript:;" onclick="uploadAction()">导入摄像头信息</button>
        <button class="btn btn-default" href="javascript:;"><a style="text-decoration:none; color:black;" id="uploadCameraModel" href="/download/uploadModel/UploadCameraInfos.xls">下载导入摄像头信息模板</a></button>

        <br/>
    </div>
    <table id="table"></table>
</div>

<script>
    // 初始化input特效
    function initMaterialInput() {
        $('form input[type="text"]').each(function () {
            if ($(this).val() != '') {
                $(this).parent().find('label').addClass('active');
            }
        });
    }

    var $table = $('#table');


    $(function() {
        init();
        //新增摄像头信息
        $("#submitbutton").click(function() {
            var classroomid = $("#classname option:selected").val();
            var deviceinfo = $("#deviceinfo").val();
            $.ajax({
                url:"/studentManageSystem/addCameraInfo",
                type:"post",
                data:{classroomid:classroomid,deviceinfo:deviceinfo},
                success:function(data){
                    if(data=="OK"){
                        $table.bootstrapTable('refresh');
                        $("#createModal").modal("hide");
                        alert("添加成功");
                    }else{
                        alert("添加失败,请重试!");
                    }
                }
            })
        });

        //更新摄像头信息
        $("#updateSubmitButton").click(function(){
            var cameraid = $("#updateCameraid").val();
            var classroomid = $("#updateClassname option:selected").val();
            var deviceinfo = $("#updateDeviceInfo").val();
            var shouldupdate = $("#shouldupdate").val();
            var height = $("#updateHeight").val();
            var width = $("#updateWidth").val();
            var checktimes = $("#updateChecktimes option:selected").val();
            var usetype = $("#updateUseType option:selected").val();

            if(height==""||width==""||$("#updateFx").val()==""||$("#updateFy").val()==""){
                alert("请填写完整数据");
                return;
            }else{
                var fx = parseFloat($("#updateFx").val());
                var fy = parseFloat($("#updateFy").val());
                var xscale = 1.0 / fx;
                var yscale = 1.0 / fy;
                height = parseInt(height);
                width = parseInt(width);
                checktimes = parseInt(checktimes);
                usetype = parseInt(usetype);

                $.ajax({
                    url:"/studentManageSystem/updateCameraInfo",
                    type:"post",
                    data:{cameraid:cameraid,classroomid:classroomid,deviceinfo:deviceinfo,shouldupdate:shouldupdate
                        ,height:height,width:width,fx:fx,fy:fy,xscale:xscale,yscale:yscale,checktimes:checktimes,usetype:usetype},
                    success:function(data){
                        if(data=="OK"){
                            alert("修改成功");
                            $table.bootstrapTable('refresh');
                            $("#updateModal").modal("hide");
                        }else{
                            alert("修改失败");
                        }
                    }
                })
            }
        });

        //上传摄像头模块
        $("#uploadbutton").click(function(){
            var fname = $("#file").val();
            var pathArray = fname.split("\\");
            if(pathArray[pathArray.length - 1].indexOf(".") == -1){
                alert("请上传文件");
                return;
            }else{
                filetype = pathArray[pathArray.length - 1].split(".")[1];

                if(filetype == "xls"){
                    var type = "file";          //后台接收时需要的参数名称，自定义即可
                    var id = "file";            //即input的id，用来寻找值
                    var formData = new FormData();
                    formData.append(type, $("#"+id)[0].files[0]);    //生成一对表单属性
                    $.ajax({
                        type: "post",           //因为是传输文件，所以必须是post
                        url:"/studentManageSystem/uploadCameraInfos",
                        data: formData,
                        processData: false,
                        contentType: false,
                        success: function (data) {
                            if(data != ""){
                                $table.bootstrapTable('refresh');
                                alert(data);
                            }else{
                                alert("导入失败");
                            }
                        }
                    });

                }else{
                    alert("请上传xls、xlsx格式文件!");
                    return;
                }
            }
        });


        function init(){
            $("#classname").empty();
            $.ajax({
                url:"/util/ClassRoominfos",
                type:"post",
                success:function (data) {
                    for(var i = 0;i<data.length;i++){
                        $("#classname").append("<option value="+data[i]['rid']+">"+data[i]['classroomname']+"</option>")
                    }
                }
            });

            $("#updateClassname").empty();
            $.ajax({
                url:"/util/ClassRoominfos",
                type:"post",
                success:function (data) {
                    for(var i = 0;i<data.length;i++){
                        $("#updateClassname").append("<option value="+data[i]['rid']+">"+data[i]['classroomname']+"</option>")
                    }
                }
            });
        }

        // bootstrap table初始化
        $table.bootstrapTable({
            url: '/studentManageSystem/getCameraInfos',
            height: getHeight(),
            striped: true,
            search: true,
            showRefresh: true,
            showColumns: true,
            minimumCountColumns: 2,
            clickToSelect: true,
            detailView: true,
            pagination: true,
            paginationLoop: false,
            sidePagination: 'server',
            silentSort: false,
            sortOrder: "desc",
            smartDisplay: false,
            escape: true,
            searchOnEnterKey: true,
            idField: 'id',
            maintainSelected: true,
            toolbar: '#toolbar',
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'cameraid', title: '摄像头编号', align: 'center'},
                {field: 'classroomid', title: '教室编号',align: 'center'},
                {field:'classroomname',title:'教室名称',align:'center'},
                {field: 'deviceinfo', title: '网络连接路径',align: 'center'},
                {field: 'shouldupdate', title: '是否需要更新数据',align: 'center'},
                {field:'height',title:'图像高度',align:'center'},
                {field: 'width', title: '图像宽度',align: 'center'},
                {field: 'fx', title: 'x轴缩放比例',align: 'center'},
                {field:'fy',title:'y轴缩放比例',align:'center'},
                {field: 'xscale', title: 'x轴图像复原处理比例',align: 'center'},
                {field: 'yscale', title: 'y轴图像复原处理比例',align: 'center'},
                {field: 'checktimes', title: '单次人脸检测次数',align: 'center'},
                {field: 'usetype', title: '使用类型',align: 'center'},
                {field: 'action', title: '操作', align: 'center', formatter: 'actionFormatter', events: 'actionEvents', clickToSelect: false}
            ]
        });
    });

    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        return [
            '<a class="update" href="javascript:;" onclick="updateAction()" data-toggle="tooltip" title="编辑"><i class="glyphicon glyphicon-edit"></i></a>　',
            '<a class="delete" href="javascript:;" onclick="deleteAction()" data-toggle="tooltip" title="删除"><i class="glyphicon glyphicon-remove"></i></a>'
        ].join('');
    }
    // 格式化时间
    function timeFormatter(value , row, index) {
        return new Date(value).toLocaleString().split(' ')[0];
    }

    function getHeight() {
        return $(window).height() - 20;
    }

    // 删除
    var deleteDialog;
    function deleteAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            deleteDialog = $.confirm({
                type: 'red',
                animationSpeed: 300,
                title: false,
                content: '确认删除该摄像头信息吗？',
                buttons: {
                    confirm: {
                        text: '确认',
                        btnClass: 'waves-effect waves-button',
                        action: function () {
                            var ids = new Array();
                            for (var i in rows) {
                                ids.push(rows[i].cameraid);
                            }
                            $.ajax({
                                type: 'get',
                                url: '/studentManageSystem/deleteCameraInfo/'+ids.join("-"),
                                success: function(result) {
                                    if(result == "OK"){
                                        deleteDialog.close();
                                        $table.bootstrapTable('refresh');
                                        alert("删除成功");
                                    }else{
                                        alert("删除失败");
                                    }
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    $.confirm({
                                        theme: 'dark',
                                        animation: 'rotateX',
                                        closeAnimation: 'rotateX',
                                        title: false,
                                        content: textStatus,
                                        buttons: {
                                            confirm: {
                                                text: '确认',
                                                btnClass: 'waves-effect waves-button waves-light'
                                            }
                                        }
                                    });
                                }
                            });
                        }
                    },
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        }
    }

    //创建摄像头信息
    function createAction(){
        $("#createModal").modal("show");
    }

    //导入摄像头信息
    function uploadAction(){
        $("#uploadModal").modal('show');
    }

    //编辑摄像头信息
    function updateAction() {
        var rows = $table.bootstrapTable('getSelections');
        if (rows.length == 0) {
            $.confirm({
                title: false,
                content: '请至少选择一条记录！',
                autoClose: 'cancel|3000',
                backgroundDismiss: true,
                buttons: {
                    cancel: {
                        text: '取消',
                        btnClass: 'waves-effect waves-button'
                    }
                }
            });
        } else {
            //赋初值
            var obj = rows[0];
            var cameraid = obj["cameraid"];
            var classroomid = obj["classroomid"];
            var classroomname = obj["classroomname"];
            var deviceinfo = obj["deviceinfo"];

            $("#updateCameraid").val(cameraid);
            $("#updateClassname option[value='"+classroomname+"']").val(classname);
            $("#updateDeviceInfo").val(deviceinfo);
            $("#shouldupdate").val(obj['shouldupdate']);
            $("#updateHeight").val(obj['height']);
            $("#updateWidth").val(obj['width']);
            $("#updateFx").val(obj['fx']);
            $("#updateFy").val(obj['fy']);
            $("#updateChecktimes option[value='"+obj['checktimes']+"']").attr("selected","selected");
            $("#updateUseType option[value='"+obj['usetype']+"']").attr("selected","selected");

            $("#updateModal").modal('show');
        }
    }

</script>

<!-- 添加摄像头信息模态框（Modal） -->
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="createModalTitle">
                    添加摄像头信息
                </h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="classname">教室名称</label>
                        <select class="form-control" id="classname" name="classname"></select>
                    </div>

                    <div class="form-group">
                        <label for="deviceinfo">设备信息</label>
                        <input class="form-control" id="deviceinfo" name="deviceinfo"/>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="submitbutton">
                    添加
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<!-- 修改摄像头信息模态框（Modal） -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="updateModalTitle">
                    编辑摄像头信息
                </h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="updateCameraid">摄像头编号</label>
                        <input class="form-control" id="updateCameraid" name="cameraid" readonly="readonly"/>
                    </div>
                    <div class="form-group">
                        <label for="updateClassname">教室名称</label>
                        <select class="form-control" id="updateClassname" name="updateClassname"></select>
                    </div>

                    <div class="form-group">
                        <label for="updateDeviceInfo">设备信息</label>
                        <input class="form-control" id="updateDeviceInfo" name="updateDeviceInfo"/>
                    </div>

                    <div class="form-group">
                        <label for="shouldupdate">是否需要更新</label>
                        <input class="form-control" id="shouldupdate" name="shouldupdate" readonly="readonly"/>
                    </div>

                    <div class="form-group">
                        <label for="updateHeight">高度</label>
                        <input class="form-control" id="updateHeight" name="updateHeight" type="number"/>
                    </div>


                    <div class="form-group">
                        <label for="updateWidth">宽度</label>
                        <input class="form-control" id="updateWidth" name="updateWidth" type="number"/>
                    </div>

                    <div class="form-group">
                        <label for="updateFx">x方向缩放比例</label>
                        <input class="form-control" id="updateFx" name="updateFx" type="text"/>
                    </div>

                    <div class="form-group">
                        <label for="updateFy">y方向缩放比例</label>
                        <input class="form-control" id="updateFy" name="updateFy" type="text"/>
                    </div>

                    <div class="form-group">
                        <label for="updateChecktimes">检测次数</label>
                        <select class="form-control" id="updateChecktimes" name="updateChecktimes">
                            <option value="1">1</option>
                            <option value="2">2</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="updateUseType">应用类型</label>
                        <select class="form-control" id="updateUseType" name="updateUseType">
                            <option value="1">1</option>
                            <option value="2">2</option>
                        </select>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="updateSubmitButton">
                    确认修改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- 导入摄像头信息模态框（Modal） -->
<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="uploadModalTitle">
                    导入摄像头信息
                </h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="file">上传文件</label>
                        <input type="file" class="form-control" id="file" name="file"/>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="uploadbutton">
                    添加
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

</body>
</html>